﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Runtime.InteropServices;

namespace MiDLL
{
    
    /*
    [Cmdlet(VerbsCommon.Get,"Saludo")]
    public class GetSaludo : PSCmdlet
    {
        private String[] nameCollection;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName  = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Nombre al que saludar."
        )]
        [Alias("Persona","NombreDePila")]
        public string[] Nombre
        {
            get { return nameCollection; }
            set { nameCollection = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            foreach (string name in nameCollection)
            {
                WriteVerbose("Creando el Saludo para " + name);
                string saludo = "Hola, " + name;
                WriteObject(saludo);
            }
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

     */

    [Cmdlet(VerbsCommon.Get, "idWindow")]
    public class idWindow : PSCmdlet
    {
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
        
        private String tituloVentana;
        //private long idVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Titulo de la ventana a buscar."
        )]
        public string Ventana
        {
            get { return tituloVentana; }
            set { tituloVentana = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Busco la ventana
            IntPtr hWnd = FindWindow(null, tituloVentana);
            WriteObject(hWnd);
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Get, "idAllWindows")]
    public class idAllWindows : PSCmdlet
    {
        private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        private static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

        private String tituloVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Titulo de las ventanas a buscar."
        )]
        public string Ventana
        {
            get { return tituloVentana; }
            set { tituloVentana = value; }
        }

        public static string GetWindowText(IntPtr hWnd)
        {
            int size = GetWindowTextLength(hWnd);
            if (size++ > 0)
            {
                var builder = new StringBuilder(size);
                GetWindowText(hWnd, builder, builder.Capacity);
                return builder.ToString();
            }

            return String.Empty;
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Busco las ventanas
            IntPtr found = IntPtr.Zero;
            List<IntPtr> windows = new List<IntPtr>();

            EnumWindows(delegate(IntPtr wnd, IntPtr param)
            {
                if (GetWindowText(wnd).Contains(tituloVentana))
                {
                    windows.Add(wnd);
                }
                return true;
            },
                        IntPtr.Zero);

            WriteObject(windows);
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Get, "idNextWindow")]
    public class idNextWindow : PSCmdlet
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        private String tituloVentana;
        private IntPtr idVentanaMadre;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Id de la ventana anterior"
        )]
        public IntPtr IdVentanaMadre
        {
            get { return idVentanaMadre; }
            set { idVentanaMadre = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 1,
            HelpMessage = "Titulo de la ventana a buscar."
        )]
        public string Ventana
        {
            get { return tituloVentana; }
            set { tituloVentana = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Busco la ventana
            IntPtr hWnd = FindWindowEx(IntPtr.Zero, idVentanaMadre, null, tituloVentana);
            WriteObject(hWnd);
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Get, "WindowIsHide")]
    public class WindowIsHide : PSCmdlet
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        private int GWL_STYLE = -16;
        private IntPtr idVentana;
        //private long idVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Id de la ventana."
        )]
        public IntPtr Ventana
        {
            get { return idVentana; }
            set { idVentana = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Saco el valor
            int valor = GetWindowLong(idVentana, GWL_STYLE);
            valor = valor & 0x10000000;
            if (valor == 0)
                WriteObject(true);
            else
                WriteObject(false);
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    /*
    [Cmdlet(VerbsCommon.Get, "idBoton")]
    public class idBoton : PSCmdlet
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        private String tituloBoton;
        private IntPtr idVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Id de la Ventana."
        )]
        public IntPtr Ventana
        {
            get { return idVentana; }
            set { idVentana = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 1,
            HelpMessage = "Ventana a buscar."
        )]
        public string Boton
        {
            get { return tituloBoton; }
            set { tituloBoton = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Busco el boton

            WriteObject(FindWindowEx(idVentana, IntPtr.Zero, "Button", tituloBoton));
         }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    */

    [Cmdlet(VerbsCommon.Set, "inFront")]
    public class inFront : PSCmdlet
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]public static extern bool SetForegroundWindow(IntPtr hWnd);

        private IntPtr idVentana;
        //private long idVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Id de la ventana a traer al frente."
        )]
        public IntPtr Ventana
        {
            get { return idVentana; }
            set { idVentana = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            WriteObject(SetForegroundWindow(idVentana));
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Set, "clickBoton")]
    public class clickBoton : PSCmdlet
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

        private IntPtr idBoton;
        //private long idVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Id del boton donde hacer click."
        )]
        public IntPtr Ventana
        {
            get { return idBoton; }
            set { idBoton = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            int BN_CLICKED = 245;
            WriteObject(SendMessage(idBoton,BN_CLICKED,0,IntPtr.Zero));
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Get, "idControl")]
    public class idControl : PSCmdlet
    {
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        public struct ListaGeneral
        {
            public IntPtr id;
            public string nombre;
            public string tipo;
        }

        static UInt32 WM_GETTEXTLENGTH = 0x000E;
        static UInt32 WM_GETTEXT = 0x000D;

        private String tituloControl;
        private String nombreClase;
        private String tituloVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Titulo de la Ventana."
        )]
        public String Ventana
        {
            get { return tituloVentana; }
            set { tituloVentana = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 1,
            HelpMessage = "Titulo del Control a buscar."
        )]
        public string Control
        {
            get { return tituloControl; }
            set { tituloControl = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 2,
            HelpMessage = "Titulo del Control a buscar."
        )]
        public string Clase
        {
            get { return nombreClase; }
            set { nombreClase = value; }
        }

        private static string GetWindowTextRaw(IntPtr hwnd)
        {
            // Allocate correct string length first
            int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null);
            StringBuilder sb = new StringBuilder(length + 1);
            SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
            return sb.ToString();
        }

        private static string GetNombreClase(IntPtr hWnd)
        {
            int nRet;
            // Pre-allocate 256 characters, since this is the maximum class name length.
            StringBuilder ClassName = new StringBuilder(256);
            //Get the window class name
            nRet = GetClassName(hWnd, ClassName, ClassName.Capacity);
            if (nRet != 0)
            {
                return (ClassName.ToString());
            }
            else
            {
                return "CLASE SIN IDENTIFICAR";
            }
        }

        static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
        {
            List<IntPtr> result = new List<IntPtr>();
            int ct = 0;
            IntPtr prevChild = IntPtr.Zero;
            IntPtr currChild = IntPtr.Zero;
            while (true && ct < maxCount)
            {
                currChild = FindWindowEx(hParent, prevChild, null, null);
                if (currChild == IntPtr.Zero) break;
                result.Add(currChild);
                prevChild = currChild;
                ++ct;
            }
            return result;
        }

        static void RecControles2(List<IntPtr> idcontrol, string TipoControl, ref List<ListaGeneral> Lista)
        {
            //Recorremos con un ciclo for each cada id que hay en la colección idcontrol
            foreach (IntPtr idcontHijo in idcontrol)
            {
                ListaGeneral regtmp;
                regtmp.id = idcontHijo;
                regtmp.nombre = GetWindowTextRaw(idcontHijo);
                regtmp.tipo = GetNombreClase(idcontHijo);
                if (TipoControl == "" || (TipoControl != "" && TipoControl == regtmp.tipo))
                {
                    Lista.Add(regtmp);
                }
                 //Si el id tiene 1 o más id's, entonces llamamos al procedimiento de forma recursiva, para que siga recorriendo los demás id's 
                List<IntPtr> Hijos = GetAllChildrenWindowHandles(idcontHijo, 100);
                if (Hijos.Count > 0) RecControles2(Hijos, TipoControl, ref Lista);
            }
        }

        static List<IntPtr> BuscaControl(string TituloVentana, string TituloControl, string TipoControl)
        {
            List<IntPtr> listaDevuelta = new List<IntPtr>();
            List<ListaGeneral> ListaFinal = new List<ListaGeneral>();
            IntPtr appHandle = FindWindow(null, TituloVentana);
            if (appHandle.ToString("X") != "0")
            {
                List<IntPtr> children = GetAllChildrenWindowHandles(appHandle, 100);
                RecControles2(children, TipoControl, ref ListaFinal);
                foreach (ListaGeneral elemento in ListaFinal)
                {
                    if (elemento.nombre == TituloControl)
                    {
                        listaDevuelta.Add(elemento.id);
                    }
                }
            }
            return listaDevuelta;
        }


        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Busco el boton
            List<IntPtr> resultados = BuscaControl(tituloVentana, tituloControl, nombreClase);
            WriteObject(resultados);
         }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Get, "idControlById")]
    public class idControlById : PSCmdlet
    {
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        public struct ListaGeneral
        {
            public IntPtr id;
            public string nombre;
            public string tipo;
        }

        static UInt32 WM_GETTEXTLENGTH = 0x000E;
        static UInt32 WM_GETTEXT = 0x000D;

        private String tituloControl;
        private String nombreClase;
        private IntPtr idVentana;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Titulo de la Ventana."
        )]
        public IntPtr Ventana
        {
            get { return idVentana; }
            set { idVentana = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 1,
            HelpMessage = "Titulo del Control a buscar."
        )]
        public string Control
        {
            get { return tituloControl; }
            set { tituloControl = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 2,
            HelpMessage = "Titulo del Control a buscar."
        )]
        public string Clase
        {
            get { return nombreClase; }
            set { nombreClase = value; }
        }

        private static string GetWindowTextRaw(IntPtr hwnd)
        {
            // Allocate correct string length first
            int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null);
            StringBuilder sb = new StringBuilder(length + 1);
            SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
            return sb.ToString();
        }

        private static string GetNombreClase(IntPtr hWnd)
        {
            int nRet;
            // Pre-allocate 256 characters, since this is the maximum class name length.
            StringBuilder ClassName = new StringBuilder(256);
            //Get the window class name
            nRet = GetClassName(hWnd, ClassName, ClassName.Capacity);
            if (nRet != 0)
            {
                return (ClassName.ToString());
            }
            else
            {
                return "CLASE SIN IDENTIFICAR";
            }
        }

        static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
        {
            List<IntPtr> result = new List<IntPtr>();
            int ct = 0;
            IntPtr prevChild = IntPtr.Zero;
            IntPtr currChild = IntPtr.Zero;
            while (true && ct < maxCount)
            {
                currChild = FindWindowEx(hParent, prevChild, null, null);
                if (currChild == IntPtr.Zero) break;
                result.Add(currChild);
                prevChild = currChild;
                ++ct;
            }
            return result;
        }

        static void RecControles2(List<IntPtr> idcontrol, string TipoControl, ref List<ListaGeneral> Lista)
        {
            //Recorremos con un ciclo for each cada id que hay en la colección idcontrol
            foreach (IntPtr idcontHijo in idcontrol)
            {
                ListaGeneral regtmp;
                regtmp.id = idcontHijo;
                regtmp.nombre = GetWindowTextRaw(idcontHijo);
                regtmp.tipo = GetNombreClase(idcontHijo);
                if (TipoControl == "" || (TipoControl != "" && TipoControl == regtmp.tipo))
                {
                    Lista.Add(regtmp);
                }
                //Si el id tiene 1 o más id's, entonces llamamos al procedimiento de forma recursiva, para que siga recorriendo los demás id's 
                List<IntPtr> Hijos = GetAllChildrenWindowHandles(idcontHijo, 100);
                if (Hijos.Count > 0) RecControles2(Hijos, TipoControl, ref Lista);
            }
        }

        static List<IntPtr> BuscaControl(IntPtr idVentana, string TituloControl, string TipoControl)
        {
            List<IntPtr> listaDevuelta = new List<IntPtr>();
            List<ListaGeneral> ListaFinal = new List<ListaGeneral>();
            if (idVentana.ToString("X") != "0")
            {
                List<IntPtr> children = GetAllChildrenWindowHandles(idVentana, 100);
                RecControles2(children, TipoControl, ref ListaFinal);
                foreach (ListaGeneral elemento in ListaFinal)
                {
                    if (elemento.nombre == TituloControl)
                    {
                        listaDevuelta.Add(elemento.id);
                    }
                }
            }
            return listaDevuelta;
        }


        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            // Busco el boton
            List<IntPtr> resultados = BuscaControl(idVentana, tituloControl, nombreClase);
            WriteObject(resultados[0]);
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

    [Cmdlet(VerbsCommon.Set, "clickBotonW")]
    public class clickBotonW : PSCmdlet
    {
        //[DllImport("user32.dll", CharSet = CharSet.Auto)]
        //public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

        [DllImport("User32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);


        public struct ListaGeneral
        {
            public IntPtr id;
            public string nombre;
            public string tipo;
        }

        private String tituloBoton;
        private String tituloVentana;

        static UInt32 WM_GETTEXTLENGTH = 0x000E;
        static UInt32 WM_GETTEXT = 0x000D;
        static UInt32 BN_CLICKED = 245;

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 0,
            HelpMessage = "Titulo del boton donde hacer click."
        )]
        public String Ventana
        {
            get { return tituloVentana; }
            set { tituloVentana = value; }
        }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true,
            Position = 1,
            HelpMessage = "Titulo de la ventana."
        )]
        public String Boton
        {
            get { return tituloBoton; }
            set { tituloBoton = value; }
        }

        private static string GetWindowTextRaw(IntPtr hwnd)
        {
            // Allocate correct string length first
            int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null);
            StringBuilder sb = new StringBuilder(length + 1);
            SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
            return sb.ToString();
        }

        private static string GetNombreClase(IntPtr hWnd)
        {
            int nRet;
            // Pre-allocate 256 characters, since this is the maximum class name length.
            StringBuilder ClassName = new StringBuilder(256);
            //Get the window class name
            nRet = GetClassName(hWnd, ClassName, ClassName.Capacity);
            if (nRet != 0)
            {
                return (ClassName.ToString());
            }
            else
            {
                return "CLASE SIN IDENTIFICAR";
            }
        }

        static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
        {
            List<IntPtr> result = new List<IntPtr>();
            int ct = 0;
            IntPtr prevChild = IntPtr.Zero;
            IntPtr currChild = IntPtr.Zero;
            while (true && ct < maxCount)
            {
                currChild = FindWindowEx(hParent, prevChild, null, null);
                if (currChild == IntPtr.Zero) break;
                result.Add(currChild);
                prevChild = currChild;
                ++ct;
            }
            return result;
        }

        static void RecControles2(List<IntPtr> idcontrol, string TipoControl, ref List<ListaGeneral> Lista)
        {
            //Recorremos con un ciclo for each cada id que hay en la colección idcontrol
            foreach (IntPtr idcontHijo in idcontrol)
            {
                ListaGeneral regtmp;
                regtmp.id = idcontHijo;
                regtmp.nombre = GetWindowTextRaw(idcontHijo);
                regtmp.tipo = GetNombreClase(idcontHijo);
                if (TipoControl == "" || (TipoControl != "" && TipoControl == regtmp.tipo))
                {
                    Lista.Add(regtmp);
                }
                //Si el id tiene 1 o más id's, entonces llamamos al procedimiento de forma recursiva, para que siga recorriendo los demás id's 
                List<IntPtr> Hijos = GetAllChildrenWindowHandles(idcontHijo, 100);
                if (Hijos.Count > 0) RecControles2(Hijos, TipoControl, ref Lista);
            }
        }

        static List<IntPtr> BuscaControl(string TituloVentana, string TituloControl, string TipoControl)
        {
            List<IntPtr> listaDevuelta = new List<IntPtr>();
            List<ListaGeneral> ListaFinal = new List<ListaGeneral>();
            IntPtr appHandle = FindWindow(null, TituloVentana);
            if (appHandle.ToString("X") != "0")
            {
                List<IntPtr> children = GetAllChildrenWindowHandles(appHandle, 100);
                RecControles2(children, TipoControl, ref ListaFinal);
                foreach (ListaGeneral elemento in ListaFinal)
                {
                    if (elemento.nombre == TituloControl)
                    {
                        listaDevuelta.Add(elemento.id);
                    }
                }
            }
            return listaDevuelta;
        }


        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            List<IntPtr> ids = BuscaControl(tituloVentana, tituloBoton, "Button");
            if (ids.Count > 0)
                WriteObject(SendMessage(ids[0], BN_CLICKED, IntPtr.Zero, null));
            else
                WriteObject("-1");
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }

}
